<?php

namespace app\api\controller;

use AlibabaCloud\Dbs\Dbs;
use app\admin\model\Cityhave;
use app\admin\model\Gupiao;
use app\admin\model\Projectcaigou;
use app\admin\model\Projectcategory;
use app\admin\model\Projectdaili;
use app\admin\model\Projectgongsi;
use app\admin\model\Projectgongying;
use app\admin\model\Projectgupiao;
use app\admin\model\Projectjijin;
use app\admin\model\Projects;
use app\admin\model\Projectuser;
use app\admin\model\Projectzhanting;
use app\admin\model\Projectzixun;
use app\admin\model\Usergupiao;
use app\admin\model\yalimedia\Teacher;
use app\common\controller\Api;
use fast\Tree;
use think\Db;

class City extends Api
{

    // 无需登录的接口,*表示全部
    //protected $noNeedLogin = ['index', 'detail','lists','caigoulist','zhanting','zhantingDetail'];
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function haveProject()
    {
        if(empty(input('project_name'))){
            $this->error('project_name必传');
        }
        $user = $this->auth->getUser();
        $input = input();
        $data['user_id'] = $user['id'];
        $data['project_name'] = $input['project_name'];
        $data['project_desc'] = $input['project_desc'];
        $data['file'] = $input['file'];
        $model = new Cityhave($data);
        $model->allowField(true)->save();
        $this->success('成功');
    }

    public function index()
    {
        $map = [
            'pid' => 0,
            'status' =>1,
        ];
        $list = \app\admin\model\City::where($map)
            ->field('id,name,image')
            ->order('sort desc')
            ->select();
        foreach($list as &$v1){
            $map = [
                'pid' => $v1['id'],
                'status' =>1,
            ];
            $list1 = \app\admin\model\City::where($map)->field('id,name,image')->order('sort desc')->select();
            foreach($list1 as &$v2){
                $map = [
                    'pid' => $v2['id'],
                    'status' =>1,
                ];
                $list2 = \app\admin\model\City::where($map)->field('id,name,image')->order('sort desc')->select();
                $v2['child'] =$list2;
                foreach ($list2 as &$v3){
                    $map = [
                        'pid' => $v3['id'],
                        'status' =>1,
                    ];
                    $list3 = \app\admin\model\City::where($map)->field('id,name,image')->order('sort desc')->select();
                    if($list3){
                        $list3 = collection($list3)->toArray();
                        $all = ['id' => $v3['id'],'image' => '','name' => '全部','status_text' => ''];
                        array_unshift($list3,$all);
                    }
                    $v3['child'] =$list3;
                }
            }
            $v1['child'] =$list1;

        }
       /* Tree::instance()->init($list);
        $list = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');*/
        $this->success('成功',$list);
    }

    public function detail()
    {
        $map = [
            'status' => 1,
            //'is_hot' => 1,
        ];
       /* if(area_id()){
            $map['area_id'] = ['in',area_id()];
        }*/
        $projects = new Projects();
        if(area_ids()){
            $projects->where(area_ids());
        }
        $project_list = $projects::where($map)
            ->field('id,project_name,image,project_desc')
            ->limit(4)
            ->order('id desc')->select();
        $projects2 = new Projects();
        if(area_ids()){
            $projects2->where(area_ids());
        }
        $project_val = $projects2->where($map)
            ->field('id,project_name,image,project_desc')->count();

        $projects3 = new Projects();
        if(area_ids()){
            $projects3->where(area_ids());
        }
        $procate = $projects3::where($map)
            ->field('id,project_type')->select();
        $cateids = [];
        if($procate){
            $cateids = array_column($procate,'project_type');
            //print_r($cateids);
            //die();
            $procate = Db::table('fa_project_category')->where('id','in',$cateids)
                ->field('id,pid')->select();
            //print_r($procate);die();
            $cateids = array_column($procate,'pid');
        }
        foreach($project_list as &$v1){
            $v1['image'] = cdnurl($v1['image'],true);
            $v1['project_desc'] = strip_tags($v1['project_desc']);
        }
        $map = [
            'project_type' => 1,
        ];
        if(area_id()){
            $map['area_id'] = ['in',area_id()];
        }
        $daili = Projectjijin::where($map)
            ->field('id,project_name,image,project_desc')
            ->limit(4)
            ->order('id desc')
            ->select();
        foreach($daili as &$v2){
            $v2['image'] = cdnurl($v2['image'],true);
            $v2['project_desc'] = strip_tags($v2['project_desc']);
        }
        $map = [
            'project_type' => 2,
        ];
        if(area_id()){
            $map['area_id'] =['in',area_id()];
        }
        $yewu = Projectjijin::where($map)
            ->field('id,project_name,image,project_desc')
            ->limit(4)
            ->order('id desc')
            ->select();
        foreach($yewu as &$v3){
            $v3['image'] = cdnurl($v3['image'],true);
            $v3['project_desc'] = strip_tags($v3['project_desc']);
        }

        if(area_id()){
           $where['area_id'] = ['in',area_id()];
        }else{
            $where = "";
        }
        $caigou = Projectcaigou::where($where)
            ->field('id,project_name,image,project_desc')
            ->limit(4)
            ->order('id desc')
            ->select();
        foreach($caigou as &$v4){
            $v4['image'] = cdnurl($v4['image'],true);
            $v4['project_desc'] = strip_tags($v4['project_desc']);
        }
        if(area_id()){
            $where['area_id'] = ['in',area_id()];
        }else{
            $where = "";
        }
        $gupiao1 = Db::table('fa_project_gupiao')->where($where)
            ->limit(6)
            ->order('id desc')
            ->select();
        $gupiao3 = collection($gupiao1)->toArray();

        $where = [
            'daili_type' => 1,
        ];
        if(area_id()){
            $where['area_id'] = ['in',area_id()];
        }
        $dali = Projectdaili::where($where)->field('id,daili_type,name,job,image')
            ->limit(2)
            ->order('id desc')
            ->select();
        foreach($dali as &$v5){
            $v5['image'] = cdnurl($v5['image'],true);
        }
        $where = [
            'status' =>1,
        ];
        if(area_id()){
            $where['area_id'] = ['in',area_id()];
        }
        $jingxiao = Projectdaili::where($where)->field('id,daili_type,name,job,image')
            ->limit(2)
            ->order('id desc')
            ->select();
        $renwu = Projectdaili::where($where)
            ->find();
        foreach($jingxiao as &$v6){
            $v6['image'] = cdnurl($v6['image'],true);
        }
        $category = new Projectcategory();
        $list = $category->where('id','in',$cateids)
            ->select();
        $cate = collection($list)->toArray();
        foreach ($cate as &$vv){
            $vv['image'] = cdnurl($vv['image'],true);
        }
        $map = [
            'is_master'=>1,
            'utype' => 0
        ];
        $Teacher = new Teacher();
        if(area_ids()){
            $Teacher->where(area_ids());
        }
        $list = $Teacher->where($map)->limit(4)->order('id desc')->select();
        $teacher = collection($list)->toArray();
        foreach($teacher as &$v){
            $v['image'] =  cdnurl($v['image'],true);
        }
        $tcate = Teacher::where($map)->where(area_ids())->field('id,cate_id')->select();
        if($tcate){
            $cate_tearch = Db::table('fa_yalimedia_teacher_cate')->where('id','in',array_column($tcate,'cate_id'))->select();
            if($cate_tearch){
                $cate_tearch =  Db::table('fa_yalimedia_teacher_cate')->where('id','in',array_column($cate_tearch,'pid'))->select();
            }

        }else{
            $cate_tearch = [];
        }
        $cate_tearch = Db::table('fa_yalimedia_teacher_cate')
            ->where("pid",0)
            ->select();
        foreach ($cate_tearch as &$vq){
            $vq['image'] =  cdnurl($vq['image'],true);
        }
        $zhan = Db::table('fa_projectzhanting')->where("")->order('id desc')->find();
        if($zhan){
            $zhan['image'] =  cdnurl($zhan['image'],true);
            $zhan['content'] =  strip_tags($zhan['content']);
        }

        if($renwu){
            $renwu['image'] =  cdnurl($renwu['image'],true);
            $renwu['project_content'] =  strip_tags($renwu['project_content']);
        }
      /*  if(area_id()){
            $gmap['area_id'] = ['in',area_id()];
        }else{
            $gmap = "";
        }*/
        $gongshi = Projectgongsi::where(area_ids())->order('id desc')->limit(2)->select();
        $gongshi = collection($gongshi)->toArray();
        foreach($gongshi as &$gs){
            $gs['image'] =  cdnurl($gs['image'],true);
            $gs['project_content'] =  strip_tags($gs['project_content']);
        }
        if(area_id()){
            $zmap['area_id'] = ['in',area_id()];
        }else{
            $zmap = "";
        }
        //$zmap= '';
        $renwulist = Db::table('fa_projectzhanting')
            ->where($zmap)
            ->order('id desc')
            ->where('utype','in',[1,2])
            ->group('utype')
            ->limit(2)->select();
        $renwulist = collection($renwulist)->toArray();
        foreach($renwulist as &$rw){
            $rw['image'] =  cdnurl($rw['image'],true);
            $rw['content'] =  strip_tags($rw['content']);
        }
        if($renwulist){
            $citycates = array_column($renwulist, 'citycate_id');

        }else{
            $citycates = [];
        }

        $citycate = Db::table('fa_citycate')
            ->where('status',1)
            //->where('id','in',$citycates)
            ->order('id desc,weigh desc')->select();
        $citycate = collection($citycate)->toArray();
        foreach($citycate as &$cc){
            $cc['image'] =  cdnurl($cc['image'],true);
        }
        $data = [
            'citycate'=>$citycate,
            'renwulist'=>$renwulist,
            'cate' => $cate,
            'project_val' => $project_val,
            'project_list'=>$project_list,
            'daili' =>$daili,
            'yewu' =>$yewu,
            'gupiao' =>$gupiao3,
            'caigou' =>$caigou,
            'citydaili' =>$dali,
            'jingxiao' =>$jingxiao,
            'teacher'=>$teacher,
            'teacher_cate'=>$cate_tearch,
            'gongshi'=>$gongshi,
            'zhanting'=>$zhan,
            'renwu' =>$renwu,
        ];
        $this->success('成功',$data);
    }

    public function jijin()
    {
        $pro = Projectjijin::get(input('id'));
        if(empty($pro)){
            $this->error('参数错误');
        }
        $pro['image'] = cdnurl($pro['image'],true);
        $imgs = explode(',',$pro['images']);
        foreach ($imgs as &$v){
            $v = cdnurl($v,true);
        }
        $pro['images_attr'] = $imgs;
        $this->success('成功',$pro);
    }

    public function caigou()
    {
        $pro = Projectcaigou::get(input('id'));
        if(empty($pro)){
            $this->error('参数错误');
        }
        $pro['image'] = cdnurl($pro['image'],true);
        $pro['kefu'] = [
            '13888888888',
            '13888888888',
        ];
        $this->success('成功',$pro);
    }

    public function daililist()
    {
        $where = '';
        if(area_id()){
            $data = Projectdaili::where('area_id','in',area_id())->paginate(input('limit',15))->toArray();
            foreach($data['data'] as &$v2){
                $v2['image'] = cdnurl( $v2['image'],true);
                $v2['project_content'] = strip_tags( $v2['project_content']);
            }
            $this->success('提交成功',$data);
        }else{
            $data = Projectdaili::where($where)->paginate(input('limit',15))->toArray();
            foreach($data['data'] as &$v2){
                $v2['image'] = cdnurl( $v2['image'],true);
                $v2['project_content'] = strip_tags( $v2['project_content']);
            }
            $this->success('提交成功',$data);
        }
    }
    public function dailidetail()
    {
        $pro = Projectdaili::get(input('id'));
        if(empty($pro)){
            $this->error('参数错误');
        }
        $pro['image'] = cdnurl($pro['image'],true);
        $this->success('提交成功',$pro);
    }

    //采购供应商
    public function gongying()
    {
        $user = $this->auth->getUser();
        $map = [
            'user_id' => $user['id'],
            'status' => 0
        ];
        $p = Projectgongying::where($map)->find();
        if($p){
            $this->error('您已提交申请');
        }
        $data = input();
        $data['user_id'] = $user['id'];
        $model = new Projectgongying($data);
        $model->allowField(true)->save();
        $this->success('提交成功');
    }

    //股权与股票
    public function gupiao()
    {
        $where = '';
        if(area_id()){
           /* $where = [];
            $l=Db::table('fa_gupiao')->where('area_id',area_id())->select();
            $ids = array_column($l,'id');*/
            $data = Projectgupiao::where('area_id','in',area_id())->paginate(input('limit',15));
            $this->success('提交成功',$data);
        }else{
            $data = Projectgupiao::where($where)->paginate(input('limit',15));
            $this->success('提交成功',$data);
        }

    }

    public function renwu()
    {
        $pro = Projectdaili::get(input('id'));
        if(empty($pro)){
            $this->error('参数错误');
        }
        $pro['image'] = cdnurl($pro['image'],true);
        $zixun = Projectzixun::where("title_type",1)
            ->order('id desc')
            ->limit(4)
            ->select();
        foreach($zixun as &$v1){
            $v1['image'] = cdnurl( $v1['image'],true);
        }
        $huodong = Projectzixun::where("title_type",2)
            ->order('id desc')
            ->limit(4)
            ->select();
        foreach($huodong as &$v2){
            $v2['image'] = cdnurl( $v2['image'],true);
        }
        $pro['zixun'] = $zixun;
        $pro['huodong'] = $huodong;

        $this->success('提交成功',$pro);
    }

    public function zixun()
    {
        $pro = Projectzixun::get(input('id'));
        if(empty($pro)){
            $this->error('参数错误');
        }
        $pro['image'] = cdnurl($pro['image'],true);
        
        $this->success('成功',$pro);
    }

    public function gupiaodetail()
    {

        $pro = Db::table('fa_gupiao')->where('id',input('id'))->find();
        if(empty($pro)){
            $this->error('参数错误');
        }
        //print_R($pro);die();
        $imgs = explode(',',$pro['images']);
        foreach ($imgs as &$v){
            $v = cdnurl($v,true);
        }
        $pro['images_attr'] = $imgs;

        $this->success('成功',$pro);
    }

    public function guquandetail()
    {
        $pro = Db::table('fa_project_gupiao')->where('id',input('id'))->find();
        if(empty($pro)){
            $this->error('参数错误');
        }
        //print_R($pro);die();
        $imgs = explode(',',$pro['images']);
        foreach ($imgs as &$v){
            $v = cdnurl($v,true);
        }
        $pro['images_attr'] = $imgs;

        $this->success('成功',$pro);
    }

    public function lists()
    {
        $where = "";
        if(input('project_type')){
            $where = [
                'project_type' => input('project_type')
            ];
            if(area_id()){
                $where += ['area_id' => ['in',area_id()]];
            }
        }
        $list = Projectjijin::where($where)->paginate(input('limit'),15)->toArray();
        foreach($list['data'] as &$v){
            $v['image'] = cdnurl($v['image'],'');
            $v['project_desc'] = strip_tags($v['project_desc']);
        }
        $this->success('成功',$list);
    }

    public function caigoulist()
    {
        $where = "";

        if(empty($where) && area_id()){
            $where = [];
            $where['area_id'] = ['in',area_id()];;
        }
        $list = Projectcaigou::where($where)->paginate(input('limit'),15)->toArray();
        foreach($list['data'] as &$v){
            $v['image'] = cdnurl($v['image'],'');
            $v['project_desc'] = strip_tags($v['project_desc']);
        }
        $this->success('成功',$list);
    }

    public function zhanting()
    {
        $where = "";
        if(empty($where) && area_id()){
            $where = [];
            $where['area_id'] = ['in',area_id()];;
            if((int)input('citycate_id')){
                $where['citycate_id'] = (int)input('citycate_id');;
            }
        }

        $list = Projectzhanting::where($where)->paginate(input('limit'),15)->toArray();
        foreach($list['data'] as &$v){
            $v['image'] = cdnurl($v['image'],true);
            $v['content'] = strip_tags($v['content']);
        }
        $this->success('成功',$list);
    }

    public function zhantingDetail()
    {
        $pro = Projectzhanting::get(input('id'));
        if(empty($pro)){
            $this->error('参数错误');
        }
        $pro['image'] = cdnurl($pro['image'],true);

        $this->success('成功',$pro);
    }

}